import {
  Body,
  Controller,
  Delete,
  Get,
  Param,
  Patch,
  Post,
  Put,
  Query,
  Req,
  Res,
  UseInterceptors,
} from '@nestjs/common';
import { Result } from '../../common/result/Result';
import { UserInfoVO } from './dto/user.response';
import { UserService } from './user.service';
import {
  UserModelAdd,
  UserModelUpdate,
  UserPageQuery,
} from './dto/user.request';
import { PageResult } from '../../common/result/PageResult';
import { SysUser } from './entities/user.entity';
import {
  AuthInterceptor,
  RequiredPermission,
} from '../../config/filter/auth.interceptor';
import { Response } from 'express';

@Controller('/api/v1/users')
export class UserController {
  constructor(private userService: UserService) {}

  /**
   * 获取当前用户登录信息
   * @param req
   */
  @Get('/me')
  @UseInterceptors(AuthInterceptor) // token验签拦截器
  async login(@Req() req): Promise<Result<UserInfoVO>> {
    const result = await this.userService.getCurrentUserInfo(req.user);
    return Result.success(result);
  }

  /**
   * 分页查询
   * @param query
   */
  @Get('/page')
  @UseInterceptors(AuthInterceptor) // token验签拦截器
  async page(@Query() query: UserPageQuery): Promise<PageResult<SysUser>> {
    const result = await this.userService.getPage(query);
    return result;
  }

  /**
   * 新增用户
   * @param body
   * @param req
   */
  @Post('')
  @UseInterceptors(AuthInterceptor) // token验签拦截器
  @RequiredPermission('sys:user:add')
  async add(@Body() body: UserModelAdd, @Req() req): Promise<Result<any>> {
    const result = await this.userService.saveUser(body, req.user);
    return result;
  }

  /**
   * 修改用户
   * @param body
   */
  @Put('/:id')
  @UseInterceptors(AuthInterceptor) // token验签拦截器
  @RequiredPermission('sys:user:edit')
  async update(@Body() body: UserModelUpdate): Promise<Result<any>> {
    const result = await this.userService.updateUser(body);
    return result;
  }

  /**
   * 删除用户
   * @param ids
   */
  @Delete('/:ids')
  @UseInterceptors(AuthInterceptor)
  @RequiredPermission('sys:user:delete')
  async delete(@Param('ids') ids: string) {
    const result = await this.userService.deleteUser(ids);
    return result;
  }

  /**
   * 用户表单信息
   * @param userId
   */
  @Get('/:userId/form')
  @UseInterceptors(AuthInterceptor)
  async getForm(@Param('userId') userId: number) {
    const result = await this.userService.getUserForm(userId);
    return result;
  }

  /**
   * 修改用户密码
   * @param userId
   * @param password
   */
  @Patch('/:userId/password')
  @UseInterceptors(AuthInterceptor)
  @RequiredPermission('sys:user:password:edit')
  async updatePassword(
    @Param('userId') userId: number,
    @Query('password') password: string,
  ) {
    const result = await this.userService.updatePassword(userId, password);
    return result;
  }

  /**
   * 修改用户状态
   * @param userId
   * @param status
   */
  @Patch('/:userId/status')
  @UseInterceptors(AuthInterceptor)
  async updateStatus(
    @Param('userId') userId: number,
    @Query('status') status: number,
  ) {
    const result = await this.userService.updateStatus(userId, status);
    return result;
  }

  @Get('/template')
  getImg(@Res() res: Response) {
    const url =
      '/Users/apple/Desktop/thkj/riverbed/riverbed-backend/public/excel-templates/用户导入模板.xlsx';
    // 正常开发中 url 应该是从数据库中的
    res.download(url, '用户导入模板.xlsx'); // 将指定路径的文件作为附件传输给浏览器
  }
}
